<?php

namespace app\command\controller;

use app\api\controller\Wechats;
use app\api\controller\Wx;
use app\common\func\Constant;
use app\common\func\Lib;
use app\common\func\Tools;
use app\common\model\FlowInfo;
use app\data\controller\Trans;
use think\Controller;

//盛付通
class Sft extends Controller
{
    protected $baseUrl = "./trade_log/sft/";

    //订单通知
    public function index()
    {
        $data = request()->param();

        if (is_string($data)) {
            $data = json_decode($data, true);
        }
        if (is_array($data)) {
            $data = $data;
        }

        if($data['busTyp'] == "1"){
            $this->binds($data);
        }else if($data['busTyp'] == "2"){
            $this->tran($data);
        }else if($data['busTyp'] == "4"){
            $this->active($data);
        }else if($data['busTyp'] == "5"){
            $this->flow($data);
        }
    }

    public function active($data){
        $sn = $data['sn'];
        $equip = Lib::getTerminal($sn);
        if($equip){
            try {
                $equip->active_time = strtotime($data['txnTm']);
                $equip->save();
            }catch (\Exception $e){
                file_put_contents($this->baseUrl."active_log".date("Ym").".txt","-------------------".date("Y-m-d H:i:s")."-----------------".PHP_EOL,FILE_APPEND | LOCK_EX);
                file_put_contents($this->baseUrl."active_log".date("Ym").".txt","info:".json_encode($data).PHP_EOL,FILE_APPEND | LOCK_EX);
                file_put_contents($this->baseUrl."active_log".date("Ym").".txt","errorInfo:".$e->getMessage().PHP_EOL,FILE_APPEND | LOCK_EX);
            }
        }
    }

    public function flow($data){
        $sn = $data['sn'];
        $equip = Lib::getTerminal($sn);
        if($equip){
            file_put_contents($this->baseUrl."sft_flow".date("Ym").".txt",json_encode($data).PHP_EOL,FILE_APPEND | LOCK_EX);
            try {
                $tradingTime = strtotime($data['txnTm']);

                $flow = new FlowInfo();
                $flow->sn = $sn;
                $flow->order_num = $data['logNo'];
                $flow->product_id = $equip->product_id;
                $flow->trading_time = $tradingTime;
                $flow->flow_money = $data['txnAmt'];
                $flow->how_many = Lib::getFlowHowMany($sn) + 1;
                $flow->save();

                Tools::sendGift($sn);

//                Tools::sendFlow($equip->user_id,$flow->id,"盛付通",$data['txnAmt'],$tradingTime);

            }catch (\Exception $e){
                file_put_contents($this->baseUrl."sft_flow_log".date("Ym").".txt","-------------------".date("Y-m-d H:i:s")."-----------------".PHP_EOL,FILE_APPEND | LOCK_EX);
                file_put_contents($this->baseUrl."sft_flow_log".date("Ym").".txt","info:".json_encode($data).PHP_EOL,FILE_APPEND | LOCK_EX);
                file_put_contents($this->baseUrl."sft_flow_log".date("Ym").".txt","errorInfo:".$e->getMessage().PHP_EOL,FILE_APPEND | LOCK_EX);
            }
        }
    }

    //商户绑定通知
    public function binds($data)
    {
        $sn = $data['snNo'];
        $equip = Lib::getTerminal($sn);
        if($equip && $equip->binding_time == 0){
            try {
                $bindTime = strtotime($data['bindTm']);
                $equip->binding_time = $bindTime;
                $equip->save();

//                Tools::operateApply($sn,$equip->user_id);//客服二部，处理完到店设备，绑定后自动完成设备申请

                Tools::sendBind($equip->user_id,date("Y-m-d H:i",$bindTime));

                \app\data\controller\Binds::instance()->customer_active($sn);

            }catch (\Exception $e){
                file_put_contents($this->baseUrl."sft_bind_log".date("Ym").".txt","-------------------".date("Y-m-d H:i:s")."-----------------".PHP_EOL,FILE_APPEND | LOCK_EX);
                file_put_contents($this->baseUrl."sft_bind_log".date("Ym").".txt","info:".json_encode($data).PHP_EOL,FILE_APPEND | LOCK_EX);
                file_put_contents($this->baseUrl."sft_bind_log".date("Ym").".txt","errorInfo:".$e->getMessage().PHP_EOL,FILE_APPEND | LOCK_EX);
            }
        }
    }

    public function tran($data){
        $sn = $data['sn'];
        $equip = Lib::getTerminal($sn);
        if($equip){
            file_put_contents($this->baseUrl."tran_data".date("Ym").".txt",json_encode($data).PHP_EOL,FILE_APPEND | LOCK_EX);
            try {
                $orderNum = $data['logNo'];
                $tranAmount = $data['txnAmt'];
                $rate = ($data['mercFeeAmt'] && $data['txnAmt']) ? round($data['mercFeeAmt']/$tranAmount,4) : 0;
                $tradingTime = strtotime($data['txnTm']);

                $sft = new \app\common\model\transaction\Sft();
                $sft->merchant_id = $data['mercId'];
                $sft->order_num = $orderNum;
                $sft->sn = $sn;
                $sft->trading_time = $tradingTime;
                $sft->transaction_amount = $tranAmount;
                $sft->transaction_fee = $data['mercFeeAmt'];
                $sft->transaction_rate = $rate * 100;
                $sft->transaction_type = isset($data['txnCd']) ? $data['txnCd'] : 0;
                $sft->card_type = isset($data['crdFlg']) ? $data['crdFlg'] : 0;
                $sft->batch_no = isset($data['batchNo']) ? $data['batchNo'] : 0;
                $sft->trace_no = isset($data['traceNo']) ? $data['traceNo'] : 0;
                $sft->bank_type = isset($data['bnkTyp']) ? $data['bnkTyp'] : 0;
                $sft->rate_type = isset($data['rateCd']) ? $data['rateCd'] : 0;
                $sft->settle_type = isset($data['stlTyp']) ? $data['stlTyp'] : 0;
                $sft->settle_time = strtotime($data['tmSmp']);
                $sft->merchant_name = is_null($equip->user_info) ? "" : $equip->user_info;
                $sft->gather_type = "NORMAL";
                $sft->save();

                Trans::instance()->customer_month($sn,$tranAmount,strtotime($data['txnTm']));

                $arriveAmount = $tranAmount - $data['mercFeeAmt'];
                $tran = \app\common\model\transaction\Sft::where(['sn' => $sn])->order("trading_time asc")->find();
                if($tran){
                    $date = strtotime(date("Y-m-d",$tran->trading_time)." 23:59:59");
                    $howMany = Lib::getFlowHowMany($sn);
                    if($tradingTime > $date && $tranAmount >= 100 && $howMany == 0){
                        $arriveAmount -= Constant::$flowSft;
                    }
                }
                Tools::sendTran($equip->user_id,$rate * 100,"盛付通",$tranAmount,$arriveAmount,date("Y-m-d H:i",$tradingTime),$sn);

            }catch (\Exception $e){
                file_put_contents($this->baseUrl."sft_tran_log".date("Ym").".txt","-------------------".date("Y-m-d H:i:s")."-----------------".PHP_EOL,FILE_APPEND | LOCK_EX);
                file_put_contents($this->baseUrl."sft_tran_log".date("Ym").".txt","data:".json_encode($data).PHP_EOL,FILE_APPEND | LOCK_EX);
                file_put_contents($this->baseUrl."sft_tran_log".date("Ym").".txt","error:".$e->getMessage().PHP_EOL,FILE_APPEND | LOCK_EX);
            }
        }
    }

}
